package com.common.dingding.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        // 定义安全方案
        SecurityScheme securityScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name("Authorization")
                .description("全局token设置，格式：Bearer <token>");
        
        // 创建安全要求
        SecurityRequirement securityRequirement = new SecurityRequirement().addList("BearerAuth");
        
        return new OpenAPI()
                .info(new Info()
                        .title("钉钉应用API文档")
                        .version("1.0.0")
                        .description("Spring Boot 3.x + MyBatis-Plus + Swagger 集成示例")
                        .contact(new Contact()
                                .name("开发者")
                                .email("developer@example.com")
                                .url("https://example.com"))
                        .license(new License()
                                .name("MIT License")
                                .url("https://opensource.org/licenses/MIT")))
                // 添加安全方案
                .components(new Components().addSecuritySchemes("BearerAuth", securityScheme))
                // 添加全局安全要求
                .addSecurityItem(securityRequirement);
    }
}